package com.ls.jpushMessageClient;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.ServiceHelper;
import cn.jiguang.common.connection.HttpProxy;
import cn.jiguang.common.connection.NettyHttpClient;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;

import java.util.HashMap;
import java.util.Map;

public class PushExample {

    public static void main(String[] args){
        String appKey="56cd3938bc6f7dc1a22e4d04";
        String masterSecret="c6115a2a692ef23464dc4415";
        JPushClient jpushClient = new JPushClient(appKey, masterSecret);
        String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
        NettyHttpClient nettyHttpClient = new NettyHttpClient(authCode,(HttpProxy) null,ClientConfig.getInstance());
        jpushClient.getPushClient().setHttpClient(nettyHttpClient);
        System.out.println("start");
        try {
            PushResult pushResult = jpushClient.sendAndroidMessageWithAlias("title", "conent", "luoyang879912");
            Map<String,String> extras=new HashMap<>();
            extras.put("msg","测试消息");
            //PushResult pushResult =jpushClient.sendAndroidNotificationWithAlias("title", "conent", extras,"luoyang879912");
            System.out.println(pushResult.statusCode);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            //e.printStackTrace();
            System.out.println("Should review the error, and fix the request:"+e.getMessage());
            System.out.println("HTTP Status: " + e.getStatus());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());
        }
        //利用 NettyHttpClient 发送保持长连接的形式发送
        // 请求结束后，调用 NettyHttpClient 中的 close 方法，否则进程不会退出。
        //jpushClient.close();
    }
}
